﻿using NetEasyMusic.Lib.Models;

namespace NetEasyMusic.Lib.Data;

public class Album : DataBasic
{
    public Album(string cookie) : base(cookie)
    {
    }

    public Album(string url, bool isUse = false) : base(url, isUse)
    {
    }

    public Album()
    {
    }

    
    /// <summary>
    /// 从专辑Id获取专辑信息
    /// </summary>
    /// <param name="id">专辑Id</param>
    /// <returns>专辑信息</returns>
    public async Task<AlbumModel> GetAlbum(string id)
    {
        var context = await GetFormString($"/album?id={id}");
        if (context == null) return new AlbumModel();
        var pic = context["album"]!["picUrl"]!.ToObject<string>();
        var name = context["album"]!["name"]!.ToObject<string>();
        return new AlbumModel
        {
            Id = id,
            Name = name!,
            Description = context["album"]!["description"]!.ToObject<string>()!,
            Type = context["album"]!["type"]!.ToObject<string>()!,
            SubType = context["album"]!["subType"]!.ToObject<string>()!,
            PicUrl = pic!,
            Songs = context["songs"]!.Select(x => new SongModel
            {
                Id = x["id"]!.ToString(),
                Artists = x["ar"]!.Select(y => new ArtistModel(y)).ToArray(),
                Album = name!,
                Name = x["name"]!.ToString(),
                AlbumId = id,
                PicUrl = pic!,
                Time = x["dt"]!.ToObject<string>()!
            }).ToArray()
        };
    }

    public async Task<SongModel[]> GetAlbumSongs(AlbumModel model)
    {
        var context = await GetFormString($"/album?id={model.Id}");
        if (context == null) return [];
        return context["songs"]!.Select(x => new SongModel
        {
            Id = x["id"]!.ToString(),
            Artists = x["ar"]!.Select(y => new ArtistModel(y)).ToArray(),
            Album = model.Name,
            Name = x["name"]!.ToString(),
            AlbumId = model.Id,
            PicUrl = model.PicUrl,
            Time = x["dt"]!.ToObject<string>()!
        }).ToArray();
    }
}